package com.ihr360.expr.engine;


import com.ihr360.expr.ExprError;
import com.ihr360.expr.ExprException;
import com.ihr360.expr.context.IEvaluationContext;
import com.ihr360.expr.core.Expr;
import com.ihr360.expr.core.ExprFunction;
import com.ihr360.expr.core.ExprVariable;
import com.ihr360.expr.data.util.Pair;

import java.util.List;
import java.util.Map;


public interface EngineProvider {
    default void addVariable(String variableId, Expr expr) {

    }

    default Expr getVariable(VariableID variable) throws ExprException{
        return null;
    }

    default   void setVariableFetcher(VariableFetcher variableFetcher){

    }
    default void removeVariable(String variableId){

    }

    default void addVariables(List<Pair<String, Expr>> pairs) {
        pairs.stream().forEach(p ->
                        addVariable(p.getFirst(), p.getSecond())
        );
    }

    /**
     * 验证变量
     *
     * @param variable
     * @throws ExprException
     */
    void validate(ExprVariable variable) throws ExprException;

    void inputChanged(VariableID variableID, Expr input);

    void valueChanged(VariableID variableID, Expr value);

    default void onError(VariableID variableID, Expr input,ExprError e){
    }

    /**
     * 函数求值
     *
     * @param context
     * @param function
     * @return
     * @throws ExprException
     */
    Expr evaluateFunction(IEvaluationContext context, ExprFunction function) throws ExprException;

    /**
     * 表达式求值
     *
     * @param context
     * @param variable
     * @return
     * @throws ExprException
     */
    Expr evaluateVariable(IEvaluationContext context, ExprVariable variable) throws ExprException;

    /**
     * 告知provider无法找到变量
     * @param variable
     */
    Expr notifyMissing(ExprVariable variable);

    /**
     * 是否提供值保存
     * @return
     */
    default boolean isValueKeeper(){
        return false;
    }

    default Expr getValue(VariableID variableID){
        return null;
    }

    default void putValue(VariableID variableID,Expr expr){
    }

    default void removeValue(VariableID variableID){

    }

    default void clear(){

    }

    default Map<VariableID, Expr> getValues(){
        return null;
    }

    default Map<String, Expr> getVariables() {
        return null;
    }

    default Map<String, Expr> getPhasedVariables() {
        return null;
    }

}
